package com.example.blog.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.blog.auth.Permission;
import com.example.blog.base.PageResult;
import com.example.blog.base.Result;
import com.example.blog.entity.SysUser;
import com.example.blog.entity.dto.user.LoginDTO;
import com.example.blog.entity.dto.user.NewMsgCount;
import com.example.blog.entity.dto.user.PageQueryDTO;
import com.example.blog.entity.dto.user.UpdateInfoDTO;
import com.example.blog.entity.vo.user.LoginUserVO;
import com.example.blog.entity.vo.user.PageQueryVO;
import com.example.blog.entity.vo.user.UserInfoVO;
import com.example.blog.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户模块
 * <p>
 * 系统用户表 前端控制器
 * </p>
 *
 * @author wlh
 * @since 2024-01-10
 */
@RestController
@RequestMapping("/blog/sysUser")
public class SysUserController {


    @Autowired
    private SysUserService sysUserService;


    /**
     * 注册
     * @param loginDTO
     * @return
     */
    @PostMapping("/register")
    public Result<Void> register(@RequestBody @Validated LoginDTO loginDTO) {
        sysUserService.register(loginDTO);
        return Result.success();
    }
    /**
     * 登录
     * @param loginDTO 入参
     * @return
     */
    @PostMapping("/login")
    public Result<LoginUserVO> login(@RequestBody @Validated LoginDTO loginDTO) {
        LoginUserVO sysUser = sysUserService.login(loginDTO);

        return Result.successData(sysUser);
    }

    /**
     * 登出
     * @return
     */
    @PostMapping("/logout")
    public Result<Void> logout() {
        sysUserService.logout();

        return Result.success();
    }


    /**
     * 后台分页查询用户列表
     * @param pageQueryDTO 入参
     * @return
     */
    @PostMapping("/admin-page")
//    @Permission("ADMIN")
    public PageResult<PageQueryVO> adminPage(@RequestBody @Validated PageQueryDTO pageQueryDTO) {
        Page<PageQueryVO> page = sysUserService.adminPage(pageQueryDTO);

        return PageResult.format(page);
    }


    /**
     * 修改接口（修改信息包括状态）
     * @param updateInfoDTO
     * @return
     */
    @PutMapping("/update")
    public Result<Void> updateInfo(@RequestBody @Validated UpdateInfoDTO updateInfoDTO) {
        sysUserService.updateInfo(updateInfoDTO);

        return Result.success();
    }


    /**
     * 删除用户
     * @param id 用户id
     * @return
     */
    @DeleteMapping("/{id}")
    @Permission("ADMIN")
    public Result<Void> del(@PathVariable Long id) {
        sysUserService.removeById(id);
        return Result.success();
    }


    /**
     * 搜索用户By nickname
     * @param value
     * @return
     */
    @GetMapping("/search")
    public PageResult<UserInfoVO> search(@RequestParam String value,
                                        @RequestParam("current") Integer current,
                                        @RequestParam("size") Integer size) {
        Page<UserInfoVO> page = sysUserService.search(value, current, size);

        return PageResult.format(page);
    }


    /**
     * 获取新的通知，例如新关注、点赞信息等
     * @return
     */
    @GetMapping("/new-msg-count")
    public Result<NewMsgCount> newMsgCount() {
        NewMsgCount newMsgCount = sysUserService.newMsgCount();

        return Result.successData(newMsgCount);
    }

    /**
     * 根据id获取用户信息
     * @param id
     * @return
     */
    @GetMapping("/getInfo/{id}")
    public Result<UserInfoVO> getById(@PathVariable Long id) {

        UserInfoVO userInfoVO = sysUserService.getUserInfo(id);

        return Result.successData(userInfoVO);
    }


    /**
     * 判断是否关注
     * @param userId
     * @return
     */
    @GetMapping("/isFollow")
    public Result<Boolean> isFollow(@RequestParam Long userId) {

        return Result.successData(sysUserService.isFollow(userId));
    }

}
